<?php
namespace Home\Controller;
use Think\Controller;

class IndexController extends Controller {
	/**
	 * 登录页面
	 */
	public function login() {
		if (IS_POST) {
			$m = M('member');
            $email = $_POST["email"];
			$password = md5($_POST["password"]);
			$user = $m ->where("email='{$email}' and password='{$password}'")->find();
			if($user){
				session_start();
				$_SESSION['uid'] = $user['id'];
				//查询用户权限
				$user['rules'] = $this->getRules($user['id']);
				$user['code'] = 0;
                $this->ajaxReturn($user);
			}else{
				$obj = (object)array();
				$obj->code=101;
				$obj->message ="用户名或密码不正确";
                $this->ajaxReturn($obj);
			}
		
		}else if(IS_GET){
		    $this -> display("login");
		}
	}
    /**
	 * 获取用户的权限
	 */
    private function getRules($uid){
    	$aga = M("auth_group_access");
		$group = $aga->field('rules')->join('think_auth_group on think_auth_group_access.group_id=think_auth_group.id')->where('uid='.$uid)->find();
		
		$rule = M("auth_rule");
		$userRules = $rule->field('id,name,title,pid')->where('id in ('.$group['rules'] .') and is_menu = 1')->select();
		return $userRules;
    }



}
